package com.boot.security.server.agency.service.impl;

import com.boot.security.server.agency.content.AgencyConstant;
import com.boot.security.server.agency.mapper.AgencyRecordMapper;
import com.boot.security.server.agency.model.domain.AgencyRecord;
import com.boot.security.server.agency.model.param.AgencyParam;
import com.boot.security.server.agency.model.vo.AgencyVo;
import com.boot.security.server.agency.service.AgencyRecordService;
import com.boot.security.server.user.mapper.SysUserMapper;
import com.boot.security.server.user.model.SysUser;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 代办业务
 * @author king
 */
@Service
public class AgencyRecordServiceImpl implements AgencyRecordService {
    @Autowired
    private AgencyRecordMapper agencyRecordMapper;
    @Autowired
    private SysUserMapper userMapper;

    @Override
    public int save (AgencyRecord agencyRecord) {
        agencyRecord.setCreateTime(new Date());
        agencyRecord.setOa(0);
        agencyRecord.setUl(0);
        agencyRecord.setEm(0);
        return agencyRecordMapper.insertSelective(agencyRecord);
    }

    @Override
    public int update (AgencyRecord agencyRecord) {
        agencyRecord.setUpdateTime(new Date());
        return agencyRecordMapper.updateByPrimaryKeySelective(agencyRecord);
    }

    @Override
    public int del (Integer id) {
        return agencyRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int delete (AgencyRecord agencyRecord) {
        return agencyRecordMapper.delete(agencyRecord);
    }

    @Override
    public List<AgencyRecord> findByAll () {
        return agencyRecordMapper.selectAll();
    }

    @Override
    public List<AgencyRecord> findByAgencyRecord (AgencyRecord agencyRecord) {
        return agencyRecordMapper.select(agencyRecord);
    }

    @Override
    public AgencyRecord findByRelation (AgencyRecord agencyRecord) {
        return agencyRecordMapper.selectOne(agencyRecord);
    }

    @Override
    public int countByAgencyRecord (AgencyRecord agencyRecord) {
        return agencyRecordMapper.selectCount(agencyRecord);
    }

    @Override
    public List<AgencyVo> findByUserNamePage (AgencyParam param) {
        SysUser user = new SysUser();
        user.setUsername(param.getUsername());
        SysUser sysUser = userMapper.selectOne(user);
        if (sysUser == null) {
            throw new RuntimeException("用户不存在，请联系管理员添加");
        }
        Integer page = Integer.valueOf(param.getPageNo());
        Integer limit = Integer.valueOf(param.getPageSize());
        Integer index = (page - 1) * limit;
        List<AgencyRecord> recordList = agencyRecordMapper.selectPage(sysUser.getId(), index, limit);
        List<AgencyVo> vos = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(recordList)) {
            recordList.forEach(agency -> {
                AgencyVo vo = new AgencyVo();
                vo.setId(agency.getId());
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                vo.setDate(format.format(agency.getCreateTime()));
                vo.setLevel("普通");
                vo.setProcess("流程");
                vo.setStatus(1);
                vo.setSysname("巡察问题督办平台");
                vo.setTitle(agency.getContent());
                vo.setUrl(String.format(AgencyConstant.URL, agency.getId()));
                vos.add(vo);
            });
        }
        ListSort(vos);
        return vos;
    }

    private static void ListSort (List<AgencyVo> list) {
        Collections.sort(list, (o1, o2) -> {
            try {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                if (format.parse(o1.getDate()).getTime() < format.parse(o2.getDate()).getTime()) {
                    return 1;//时间越近越在前面
                } else {
                    return - 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 0;
        });
    }
}
